package com.edu.dao.impl;

import com.edu.dao.AddressesDao;
import com.edu.entity.Address;
import com.edu.entity.AddressEntity;
import com.edu.utils.DatabaseManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * com.edu.dao.impl
 * 2024/7/15 8:54
 *
 * @author Fancy
 * @version 1.0
 */
public class AddressesDaoImpl implements AddressesDao {
    private Connection connection;

    //jdbc操作连接
    public AddressesDaoImpl(Connection connection) {
        this.connection = connection;
    }

    @Override
    public Integer addAddress(AddressEntity address, Integer addAddressDetailId, int userId) {
        String sql = "insert into address(user_id, receiver_name, address_detail, is_default) values(?, ?, ?, ?)";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, userId);
            preparedStatement.setString(2, address.getReceiverName());
            preparedStatement.setInt(3, addAddressDetailId);
            preparedStatement.setBoolean(4, address.isDefault());
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Address> getAddressesByUserId(int userId) {
        String sql = "select * from address where user_id = ?";

        List<Address> addressList = new ArrayList<>();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, userId);
            ResultSet resultSet = preparedStatement.executeQuery();
            while(resultSet.next())
            {
                Address address = new Address();
                address.setAddressId(resultSet.getInt("address_id"));
                address.setReceiverName(resultSet.getString("receiver_name"));
                address.setAddressDetailId(resultSet.getInt("address_detail"));
                address.setIsDefault(resultSet.getBoolean("is_default"));
                addressList.add(address);
            }
            return addressList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public int deleteAddressById(int addressId) {
        String sql = "delete from address where address_id = ?";
        try {
            PreparedStatement preparedStatement = null;
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, addressId);
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public int updateReverseName(int addressId, AddressEntity address) {
        String sql = "update address set receiver_name = ? where address_id = ?";
            try {
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, address.getReceiverName());
                preparedStatement.setInt(2, addressId);
                return preparedStatement.executeUpdate();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

}
